﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TinyWnd;

public class RankItemData
{
    public string spriteName;
    public string lblName;
}

public class WndRankContent : WndContentData
{
    public List<RankItemData> lst = new List<RankItemData>();
}

public class RankWnd : Wnd
{
    [SerializeField]
    GameObject prefabRankItem;

    UIGrid grid;
    WRankDetail partDetail;

    protected override void OnAwake()
    {
        _wndId = WndId.rank;
        _unifiedClose = true;
        _colliderMode = WndColliderMode.transparent;

        var tran = transform;
        grid = Utility.FindChild<UIGrid>(tran, "Left/Rank/Bg/Scroll View/Grid");
        partDetail = Utility.FindChild<WRankDetail>(tran, "Left/Detail");
    }

    IEnumerator FillRankItem(List<RankItemData> lst)
    {
        var tran = grid.transform;
        var go = grid.gameObject;
        for (var i = 0; i < tran.childCount; i++)
            Destroy(tran.GetChild(i).gameObject);
        yield return new WaitForEndOfFrame();

        foreach (var one in lst)
        {
            GameObject item = NGUITools.AddChild(go, prefabRankItem);
            WRankItemTemp script = item.GetComponent<WRankItemTemp>();
            script.InitItem(one);
        }

        grid.repositionNow = true;
    }

    protected override void OnShow(WndContentData content)
    {
        gameObject.SetActive(true);
        var data = content as WndRankContent;
        if (data != null)
            StartCoroutine(FillRankItem(data.lst));
    }

    public void ShowRankDetail(RankItemData data)
    {
        partDetail.InitDetail(data);
    }

    public void HideRankDetail()
    {
        partDetail.HideDetail();
    }
}
